home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Printing / Dashed Lines / Dashed Lines.p < prev    next >
Encoding:
Text File  |  1992-07-15  |  5.3 KB  |  216 lines  |  [TEXT/MPS ]

  1. {**
  2.  **     Program: Dashed Lines
  3.  **
  4.  **     Version: 1.0    4/91
  5.  **
  6.  **        MPW 3.2 Pascal source
  7.  **
  8.  **     Purpose:
  9.  **
  10.  **        Dashed Lines demonstrates how to draw dashed line objects on PostScript printers.
  11.  **        This simple example does not deal with QuickDraw printers, on which it draws solid
  12.  **        black lines.
  13.  **        
  14.  **        - Dave Hersey 4/23/91
  15.  **
  16.  **        Macintosh Developer Technical Support
  17.  **
  18.  **
  19.  **}
  20.  
  21. PROGRAM Dashed_Lines;
  22.  
  23. USES
  24.     OSIntf, QuickDraw, ToolIntf, MacPrint;
  25.  
  26.  
  27. CONST
  28.     DashedLine            = 180;    (*    Begin PostScript line dashing            *)
  29.     DashedStop            = 181;    (*    End PostScript line dashing                *)
  30.     SetLineWidth        = 182;    (*    Set hi resolution line width             *)    
  31.  
  32. TYPE
  33.     TDashedLineHdl = ^TDashedLinePtr;
  34.     TDashedLinePtr = ^TDashedLine;
  35.     TDashedLine = PACKED RECORD
  36.         offset:        SignedByte;
  37.         centered:    SignedByte;
  38.         dashed:        ARRAY [0..1] OF SignedByte;
  39.     END;
  40.  
  41.  
  42.   {*------ DrawStuff -----------------------------------------------------------------*}
  43.  
  44. {**
  45.  **      DrawStuff draws QuickDraw objects with dashed lines in Postscript.
  46.  **}
  47.  
  48.  PROCEDURE DrawStuff (theWorld : Rect; theGPort : GrafPtr);
  49.  
  50. TYPE
  51.     widhdl = ^widptr;
  52.     widptr = ^widpt;
  53.     widpt = Point;
  54.  
  55.  VAR
  56.    oldPort      :     GrafPtr;
  57.    PSHdl        :    Handle;
  58.    arect        :    Rect;
  59.    Width        :    Widhdl;
  60.    dashedln     :    TDashedLineHdl;
  61.    myPic        :    PicHandle;
  62.  
  63.  
  64. BEGIN
  65.     GetPort (oldPort);
  66.  
  67.     SetPort (theGPort);
  68.     Dashedln := TDashedLineHdl(NewHandle(sizeof(tdashedline)));
  69.     Dashedln^^.offset := 0;       {No offset}
  70.     Dashedln^^.centered := 0;     {don’t center}
  71.     Dashedln^^.dashed[0] := 1;    {this is the length }
  72.     Dashedln^^.dashed[1] := 4;    {this means 4 points on, 4 points off }
  73.     
  74.     Width := widhdl(NewHandle(sizeof(widpt)));
  75.     Width^^.h := 4;                {denominator is 4}
  76.     Width^^.v := 1;               {numerator is 1}
  77.     
  78.     myPic := OpenPicture(theWorld);
  79.     PenSize(1,1);                {Set the pen size to 1 wide x 1 high }
  80.     ClipRect(theWorld);
  81.  
  82.     PicComment(DashedLine,GetHandleSize(Handle(dashedln)),Handle(dashedln)); 
  83.     PicComment(SetLineWidth,4,Handle(width));   {SetLineWidth to 1 pixel wide.}
  84.  
  85.     SetRect(arect,100,100,500,500);    {Draw some stuff in dash mode.}
  86.     FrameRect(aRect);
  87.     MoveTo(500,500);
  88.     Lineto(100,100);
  89.     MoveTo(100,500);
  90.     Lineto(500,100);
  91.     InsetRect(arect,10,10);
  92.     FrameOval(aRect);
  93.     InsetRect(arect,10,10);
  94.     FrameOval(aRect);
  95.     InsetRect(arect,10,10);
  96.     FrameOval(aRect);
  97.     InsetRect(arect,10,10);
  98.     FrameOval(aRect);
  99.     InsetRect(arect,10,10);
  100.     FrameOval(aRect);
  101.     InsetRect(arect,10,10);
  102.     FrameOval(aRect);
  103.     InsetRect(arect,10,10);
  104.     FrameOval(aRect);
  105.     InsetRect(arect,10,10);
  106.     FrameOval(aRect);
  107.     InsetRect(arect,10,10);
  108.     FrameOval(aRect);
  109.     PicComment(DashedStop,0,nil);    {DashedStop}
  110.  
  111.   ClosePicture;
  112.   DisposHandle(handle(width));           {Clean up}
  113.   DisposHandle(handle(dashedln));
  114.   DrawPicture(MyPic, theWorld);          {print it}
  115.   KillPicture(MyPic);    SetPort(oldPort);
  116.  
  117.  END;  {**  DrawStuff  **}
  118.  
  119.  
  120. {*------ PrintStuff ----------------------------------------------------------------*}
  121. {**
  122.  **        PrintStuff will call all of the nescessary Print Manager calls to print 
  123.  **        a document. It checks PrError() after each Print Manager call. If an error 
  124.  **     is found, all of the Print Manager open calls (i.e. PrOpen, PrOpenDoc...) 
  125.  **        will have a corresponding close call before the error is posted to the user. 
  126.  **        You want to use this approach to make sure the Print Manager closes properly 
  127.  **        and all temporary memory is released.
  128.  **}
  129.  
  130. PROCEDURE PrintStuff;
  131.  
  132. VAR
  133.   Loop,
  134.   NumberOfPages,
  135.   PageNumber        : Integer;
  136.   PrintError        : LongInt;
  137.   oldPort              : GrafPtr;
  138.   thePrRecHdl        : THPrint;
  139.   thePrPort            : TPPrPort;
  140.   theStatus            : TPrStatus;
  141.     
  142. BEGIN
  143.    GetPort(oldPort);
  144.     
  145.    thePrRecHdl := THPrint(NewHandle(SIZEOF(TPrint)));
  146.     
  147.    IF (MemError = noErr) AND (thePrRecHdl <> NIL) THEN
  148.     BEGIN
  149.        PrOpen;
  150.        IF (PrError = noErr) THEN
  151.         BEGIN
  152.            PrintDefault(thePrRecHdl);
  153.  
  154.            IF (PrError = noErr) THEN
  155.             BEGIN
  156.                IF (PrStlDialog(thePrRecHdl)) THEN
  157.                 BEGIN
  158.                    IF (PrJobDialog(thePrRecHdl)) THEN 
  159.                     BEGIN
  160.                           thePrPort := PrOpenDoc(thePrRecHdl, NIL, NIL);
  161.                                
  162.                       IF (PrError = noErr) THEN
  163.                         BEGIN
  164.  
  165.                              PrOpenPage(thePrPort, NIL);
  166.                                 
  167.                           IF (PrError = noErr) THEN
  168.                             BEGIN
  169.                               {**
  170.                                   rPage (IM II-150) is the printable area for the  
  171.                                   currently selected printer. By passing the current  
  172.                                   port to the draw routine, enables your app
  173.                                   to use the same routine to draw to the screen
  174.                                   and the printer's GrafPort.
  175.                                **}
  176.                                     
  177.                                DrawStuff (thePrRecHdl^^.prInfo.rPage, 
  178.                                            GrafPtr (thePrPort));
  179.                                  
  180.                              END;
  181.                             PrClosePage(thePrPort);
  182.                           END;
  183.                              
  184.                           PrCloseDoc(thePrPort);
  185.                              
  186.                           IF (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) THEN
  187.                                PrPicFile(thePrRecHdl, NIL, NIL, NIL, theStatus);
  188.  
  189.                       END;
  190.                   END;
  191.               END;
  192.           END;
  193.         
  194.         PrClose;
  195.  
  196.      END;
  197.  
  198. END;  {**  PrintStuff  **}
  199.  
  200.  
  201. {*------ main ----------------------------------------------------------------------*}
  202.  
  203. BEGIN
  204.     
  205.     InitGraf(@thePort);
  206.     InitFonts;
  207.     FlushEvents(everyEvent, 0);    
  208.     InitWindows;
  209.     InitMenus;
  210.     TEInit;
  211.     InitDialogs(NIL);
  212.     InitCursor;
  213.  
  214.     PrintStuff;
  215.  
  216. END. {**  main  **}